var CMTouchLayer = cc.Layer.extend({
	clean : false,
	ctor : function(){
		this._super();
		this.initMenu();
	},
	initMenu : function() {
		var level = parseInt(getLevelData());
		console.log("level:"+level);
		if(level <= 23)
			level += 1;
		var newGame=new Array();
		var newGameNormal = new Array();
		var newGameSelected= new Array();
		var newGameDisabled= new Array();
		var menu = new Array();
		for(var i = 0;i < level;i++){
		(function(idx,a){
			newGameNormal[idx] = new cc.Sprite("res/move.png", cc.rect(800, 200, 100, 100));
			newGameSelected[idx] = new cc.Sprite("res/move.png", cc.rect(800, 200, 100, 100));
			newGameDisabled[idx] = new cc.Sprite("res/move.png", cc.rect(800, 200, 100, 100));
			var temp = function(){
				if(!clean){
					_level=idx;
					a.removeAllChildren(true);
					cc.director.runScene(new cc.TransitionPageTurn(1.2, new GameBoardScene(),false));
				}
			};
			
			newGame[idx] = new cc.MenuItemSprite(
					newGameNormal[idx],newGameSelected[idx],newGameDisabled[idx],
					temp,a);
		})(i,this)
		}
		for(var i = 0;i < level/6;i++){
			menu[i]=new cc.Menu();
			if((i+1)*6<level||level%6==0)
				menu[i].setPosition(320,960-150-(i*100));
			else
				menu[i].setPosition(20+(50*(level%6)),960-150-(i*100));
			for(var j = 0;j < 6&&j<level-(i*6);j++)
			{
				menu[i].addChild(newGame[i*6+j], 1, "botton"+(i*6+j));
			}
			menu[i].alignItemsHorizontallyWithPadding(0);
			this.addChild(menu[i]);
		}
		for(var i =0;i<level;i++){
			var done = getLevelDone(i);
			if(done == "done"){
				var done = new cc.Sprite("res/move.png", cc.rect(900, 200, 100, 100));
				done.setPosition(70+(100*(i%6)),960-150-((i-(i%6))/6*100));
				this.addChild(done);
			}
		}
		
		var downMenu = new cc.Menu();
		var frontMenu = new cc.Menu();
		MenuRes = "res/Menu.png";
		MoveRes = "res/move.png";
		UIRes = "res/UiModule.png";
		var clean = false;
		var language = "cn";
		if(language == "cn"){
			var languageMenuNormal = new cc.Sprite(MenuRes, cc.rect(0, 600, 320, 100));
			var languageMenuSelected = new cc.Sprite(MenuRes, cc.rect(0, 600, 320, 100));
			var languageMenuDisabled = new cc.Sprite(MenuRes, cc.rect(0, 600, 320, 100));
		}
		var languageMenu = new cc.MenuItemSprite(
				languageMenuNormal,languageMenuSelected,languageMenuDisabled,
				languageAction,this);
		var languageAction = function(){
		};
		
		var skipRead = getSettingData("skipRead");
		if(skipRead == "false"){
			var skipReadNormal = new cc.Sprite(MenuRes, cc.rect(0, 700, 320, 100));
			var skipReadSelected = new cc.Sprite(MenuRes, cc.rect(0, 700, 320, 100));
			var skipReadDisabled = new cc.Sprite(MenuRes, cc.rect(0, 700, 320, 100));
		}
		if(skipRead == "true"){
			var skipReadNormal = new cc.Sprite(MenuRes, cc.rect(320, 700, 320, 100));
			var skipReadSelected = new cc.Sprite(MenuRes, cc.rect(320, 700, 320, 100));
			var skipReadDisabled = new cc.Sprite(MenuRes, cc.rect(320, 700, 320, 100));
		}
		var skipReadAction = function(){
			if(getSettingData("skipRead") == "false"){
				setSettingData("skipRead","true");
				skipReadMenu.setNormalImage(new cc.Sprite(MenuRes, cc.rect(320, 700, 320, 100)));
				skipReadMenu.setSelectedImage(new cc.Sprite(MenuRes, cc.rect(320, 700, 320, 100)));
				skipReadMenu.setDisabledImage(new cc.Sprite(MenuRes, cc.rect(320, 700, 320, 100)));
			}
			else{
				setSettingData("skipRead","false");
				skipReadMenu.setNormalImage(new cc.Sprite(MenuRes, cc.rect(0, 700, 320, 100)));
				skipReadMenu.setSelectedImage(new cc.Sprite(MenuRes, cc.rect(0, 700, 320, 100)));
				skipReadMenu.setDisabledImage(new cc.Sprite(MenuRes, cc.rect(0, 700, 320, 100)));
			}
		};
		var skipReadMenu = new cc.MenuItemSprite(
				skipReadNormal,skipReadSelected,skipReadDisabled,
				skipReadAction,this);
		
		var skipAll = getSettingData("skipAll");
		if(skipAll == "false"){
			var skipAllNormal = new cc.Sprite(MenuRes, cc.rect(0, 800, 320, 100));
			var skipAllSelected = new cc.Sprite(MenuRes, cc.rect(0, 800, 320, 100));
			var skipAllDisabled = new cc.Sprite(MenuRes, cc.rect(0, 800, 320, 100));
		}
		if(skipAll == "true"){
			var skipAllNormal = new cc.Sprite(MenuRes, cc.rect(320, 800, 320, 100));
			var skipAllSelected = new cc.Sprite(MenuRes, cc.rect(320, 800, 320, 100));
			var skipAllDisabled = new cc.Sprite(MenuRes, cc.rect(320, 800, 320, 100));
		}
		var skipAllAction = function(){
			if(getSettingData("skipAll") == "false"){
				setSettingData("skipAll","true");
				skipAllMenu.setNormalImage(new cc.Sprite(MenuRes, cc.rect(320, 800, 320, 100)));
				skipAllMenu.setSelectedImage(new cc.Sprite(MenuRes, cc.rect(320, 800, 320, 100)));
				skipAllMenu.setDisabledImage(new cc.Sprite(MenuRes, cc.rect(320, 800, 320, 100)));
			}
			else{
				setSettingData("skipAll","false");
				skipAllMenu.setNormalImage(new cc.Sprite(MenuRes, cc.rect(0, 800, 320, 100)));
				skipAllMenu.setSelectedImage(new cc.Sprite(MenuRes, cc.rect(0, 800, 320, 100)));
				skipAllMenu.setDisabledImage(new cc.Sprite(MenuRes, cc.rect(0, 800, 320, 100)));
			}
		};
		var skipAllMenu = new cc.MenuItemSprite(
				skipAllNormal,skipAllSelected,skipAllDisabled,
				skipAllAction,this);
		
		var ignoreFail = getSettingData("ignoreFail");
		if(ignoreFail == ""){
			ignoreFail = "false";
			setSettingData("ignoreFail","false");
		}
		if(ignoreFail == "false"){
			var ignoreFailNormal = new cc.Sprite(MenuRes, cc.rect(0, 900, 320, 100));
			var ignoreFailSelected = new cc.Sprite(MenuRes, cc.rect(0, 900, 320, 100));
			var ignoreFailDisabled = new cc.Sprite(MenuRes, cc.rect(0, 900, 320, 100));
		}
		if(ignoreFail == "true"){
			var ignoreFailNormal = new cc.Sprite(MenuRes, cc.rect(320, 900, 320, 100));
			var ignoreFailSelected = new cc.Sprite(MenuRes, cc.rect(320, 900, 320, 100));
			var ignoreFailDisabled = new cc.Sprite(MenuRes, cc.rect(320, 900, 320, 100));
		}
		var ignoreFailAction = function(){
			if(getSettingData("ignoreFail") == "false"){
				setSettingData("ignoreFail","true");
				ignoreFailMenu.setNormalImage(new cc.Sprite(MenuRes, cc.rect(320, 900, 320, 100)));
				ignoreFailMenu.setSelectedImage(new cc.Sprite(MenuRes, cc.rect(320, 900, 320, 100)));
				ignoreFailMenu.setDisabledImage(new cc.Sprite(MenuRes, cc.rect(320, 900, 320, 100)));
			}
			else{
				setSettingData("ignoreFail","false");
				ignoreFailMenu.setNormalImage(new cc.Sprite(MenuRes, cc.rect(0, 900, 320, 100)));
				ignoreFailMenu.setSelectedImage(new cc.Sprite(MenuRes, cc.rect(0, 900, 320, 100)));
				ignoreFailMenu.setDisabledImage(new cc.Sprite(MenuRes, cc.rect(0, 900, 320, 100)));
			}
		};
		var ignoreFailMenu = new cc.MenuItemSprite(
				ignoreFailNormal,ignoreFailSelected,ignoreFailDisabled,
				ignoreFailAction,this);
		
		var returnNormal = new cc.Sprite(MoveRes, cc.rect(500, 200, 100, 100));
		var returnSelected = new cc.Sprite(MoveRes, cc.rect(500, 200, 100, 100));
		var returnDisabled = new cc.Sprite(MoveRes, cc.rect(500, 200, 100, 100));
		
		var returnAction = function(){
			cc.director.runScene(new cc.TransitionPageTurn(1.2, new MainMenuScene(),true));
		};
		var returnMenu = new cc.MenuItemSprite(
				returnNormal,returnSelected,returnDisabled,
				returnAction,this);
		var cleanNormal = new cc.Sprite(MoveRes, cc.rect(800, 100, 100, 100));
		var cleanSelected = new cc.Sprite(MoveRes, cc.rect(800, 100, 100, 100));
		var cleanDisabled = new cc.Sprite(MoveRes, cc.rect(800, 100, 100, 100));

		var cleanAction = function(){
				downMenu.enabled = false;
				clean = true;
				var dealog = new cc.Sprite(UIRes, cc.rect(0, 400, 640, 200));
				var acceptNormal = new cc.Sprite(MoveRes, cc.rect(900, 100, 100, 50));
				var acceptSelected = new cc.Sprite(MoveRes, cc.rect(900, 100, 100, 50));
				var acceptDisabled = new cc.Sprite(MoveRes, cc.rect(900, 100, 100, 50));
				var cancelNormal = new cc.Sprite(MoveRes, cc.rect(900, 150, 100, 50));
				var cancelSelected = new cc.Sprite(MoveRes, cc.rect(900, 150, 100, 50));
				var cancelDisabled = new cc.Sprite(MoveRes, cc.rect(900, 150, 100, 50));
				var text = new cc.LabelTTF("","res/font/Microsoft Yahei.ttf", 25,cc.size(375,200), cc.TEXT_ALIGNMENT_LEFT);
				text.attr({
					anchorX : 0.5,
					anchorY : 0.5,
					x: 320,
					y: 960-420,
					color : cc.color(0, 0, 0)
				});
				text.setString("接下来会清除掉所有游戏记录，你确定要这样做吗？");
				
				var acceptAction = function(){
					frontMenu.removeChildByTag(102, true);
					frontMenu.removeChildByTag(103, true);
					text.setString("数据清除中，请勿关闭游戏。");
					this.scheduleOnce(function(){  
						resetSaveData();
						cc.director.runScene(new cc.TransitionPageTurn(1.2, new MainMenuScene(),true));
					}, 3);  
				}
				
				var cancelAction = function(){
					this.removeChildByTag(100, true);
					this.removeChildByTag(101, true);
					frontMenu.removeChildByTag(102, true);
					frontMenu.removeChildByTag(103, true);
					downMenu.enabled = true;
					clean = false;
				}
				
				dealog.setPosition(320,960-400);
				this.addChild(dealog,99,100);
				this.addChild(text,101,101);
				var acceptMenu = new cc.MenuItemSprite(
						acceptNormal,acceptSelected,acceptDisabled,
						acceptAction,this);
				acceptMenu.setPosition(200,960-450);
				frontMenu.addChild(acceptMenu,99,102);
				
				var cancelMenu = new cc.MenuItemSprite(
						cancelNormal,cancelSelected,cancelDisabled,
						cancelAction,this);
				cancelMenu.setPosition(440,960-450);
				frontMenu.addChild(cancelMenu,99,103);
		};
		
		var cleanMenu = new cc.MenuItemSprite(
				cleanNormal,cleanSelected,cleanDisabled,
				cleanAction,this);
		
		downMenu.setPosition(0, 0);
		frontMenu.setPosition(0, 0);
// languageMenu.setPosition(160,960-150-400);
// downMenu.addChild(languageMenu);
		skipReadMenu.setPosition(320+160,960-150-400);
		downMenu.addChild(skipReadMenu);
		skipAllMenu.setPosition(320+160,960-150-500);
		downMenu.addChild(skipAllMenu);
		ignoreFailMenu.setPosition(320+160,960-150-600);
		downMenu.addChild(ignoreFailMenu);
		returnMenu.setPosition(75,960-150-700);
		downMenu.addChild(returnMenu);
		cleanMenu.setPosition(225,960-150-700);
		downMenu.addChild(cleanMenu);
		this.addChild(downMenu);
		this.addChild(frontMenu,100,"f");
	},
}); 